package sicnu.cs.ich.ich.config;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import sicnu.cs.ich.api.common.Constants;
import sicnu.cs.ich.common.services.impl.RedisService;
import sicnu.cs.ich.ich.feign.IchRankingClient;
import sicnu.cs.ich.ich.service.IIchService;

import java.util.List;

/**
 * @author CaiKe
 * @date 2021/12/9 17:19
 */
@Slf4j
@Component
@Order(value = 1)
@RequiredArgsConstructor
public class ListenerConfig implements ApplicationRunner {
    private final IIchService ichService;
    private final IchRankingClient rankingClient;
    private final RedisService redisService;

    @Override
    public void run(ApplicationArguments args) throws InterruptedException {
        // 从Ich_RANKING中获取最新的热点数据
        List<Integer> ids = null;
        try {
             ids = rankingClient.sortAndGet();
        }catch (RuntimeException e){
            log.error("ich-Ranking服务可能没启动");
//            e.printStackTrace();
            return;
        }
        redisService.cacheList(Constants.REDIS_RANK_ICH_HIT, ids);
        ichService.cacheIchs(ids, 2 * 60 * 1000L);
    }


}